package com.jp.knowledge.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.activity.InfoDataDetailActivity;
import com.jp.knowledge.adapter.OnItemListerner;
import com.jp.knowledge.adapter.SearchCommtemAdapter;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.HeadLineData;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/3/20.
 */

public class AttentionDynamicFragment extends BaseFragment implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener, ResultSubscriber.OnResultListener, OnItemListerner {

    protected static final int REFRESH_CODE = 0x01;
    protected static final int LOADMORE_CODE = 0x02;

    protected String id;
    protected int type;
    private String navTypeId;
    protected int page;
    protected Context context;
    protected CanRefreshLayout refreshView;
    protected RecyclerView contentRv;

    protected List<HeadLineData> headLineDatas;
    protected SearchCommtemAdapter adapter;

    public static AttentionDynamicFragment newInstance(String id, int type, String navTypeId){
        AttentionDynamicFragment fragment = new AttentionDynamicFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("id", id);
        bundle.putSerializable("type", type);
        bundle.putSerializable("navTypeId", navTypeId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context = getContext();
        Bundle bundle = getArguments();
        this.id = bundle.getString("id");
        this.type = bundle.getInt("type", 9);
        this.navTypeId = bundle.getString("navTypeId");
        this.page = 0;

        headLineDatas = new ArrayList<>();
        adapter = new SearchCommtemAdapter(context, headLineDatas);
        adapter.setOnItemListener(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        refreshView = (CanRefreshLayout) View.inflate(getContext(), R.layout.refresh_recycleview, null);
        contentRv = (RecyclerView) refreshView.findViewById(R.id.can_content_view);
        refreshView.setOnRefreshListener(this);
        refreshView.setOnLoadMoreListener(this);
        refreshView.setLoadMoreEnabled(false);

        contentRv.setHasFixedSize(true);
        contentRv.setLayoutManager(new LinearLayoutManager(context));
        contentRv.setAdapter(adapter);

        refreshView.autoRefresh();
        return refreshView;
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(mContext, InfoDataDetailActivity.class);
        intent.putExtra("id", headLineDatas.get(position).getId());
        intent.putExtra("type", headLineDatas.get(position).getType());
        intent.putExtra("linkTitle", headLineDatas.get(position).getTitle());
        try {
            intent.putExtra("linkIcon", headLineDatas.get(position).getImglist().get(0).getSrc());
        } catch (Exception e){}finally {
            startActivity(intent);
        }
    }

    @Override
    public void onRefresh() {
        page = 0;
        getData(++page, REFRESH_CODE);
    }

    @Override
    public void onLoadMore() {
        getData(++page, LOADMORE_CODE);
    }

    protected void getData(int page, int model){
        JsonObject param = new JsonObject();
        param.addProperty("type", type);
        param.addProperty("page", page);
        param.addProperty("id", id);
        if(model == REFRESH_CODE) {
            param.addProperty("time", System.currentTimeMillis());
        } else if(model == LOADMORE_CODE) {
            if(headLineDatas.size() == 0)
                param.addProperty("time", System.currentTimeMillis());
            else
                param.addProperty("time", headLineDatas.get(headLineDatas.size() - 1).getCreatetime());
        }
        if(navTypeId != null)
            param.addProperty("navTypeId", navTypeId);
        HttpHelper.getInstance(mContext).attentionGroupDetailList(param, model, this);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        refreshView.refreshComplete();
        refreshView.loadMoreComplete();
    }
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            onError(requestType);
            ToasUtil.toast(mContext, data.getMessage());
        } else if(requestType == REFRESH_CODE || requestType == LOADMORE_CODE){
            if(requestType == REFRESH_CODE)
                headLineDatas.clear();
            List<HeadLineData> headLineDataList = data.getList(HeadLineData.class);
            if(headLineDataList != null && headLineDataList.size() > 0)
                headLineDatas.addAll(headLineDataList);
            adapter.updataAdapter(headLineDatas);
        }
    }
}
